package practice.hot100;

import java.util.List;

public class Solution02 {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int cur = 0;
        ListNode dummy = new ListNode(-1);
        ListNode p = dummy;
        while (l1 != null || l2 != null) {
            int x = l1 == null ? 0 : l1.val;
            int y = l2 == null ? 0 : l2.val;
            int tmp = x + y + cur;
            p.next = new ListNode(tmp % 10);
            p = p.next;
            cur = tmp / 10;
            if (l1 != null) l1 = l1.next;
            if (l2 != null) l2 = l2.next;
        }
        if (cur != 0) p.next = new ListNode(cur);
        return dummy.next;
    }
}

class ListNode {
    int val;
    ListNode next;

    ListNode() {
    }

    ListNode(int val) {
        this.val = val;
    }

    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}
